package com.bai.localshop.controller.user;

import com.bai.localshop.exception.BaseException;
import com.bai.localshop.pojo.dto.ProductQueryDTO;
import com.bai.localshop.pojo.vo.ProductVO;
import com.bai.localshop.pojo.vo.UserProductPageVO;
import com.bai.localshop.result.Result;
import com.bai.localshop.service.ProductService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户端商品控制器
 */
@RestController("userProductController")
@RequestMapping("/api/user/products")
@RequiredArgsConstructor
@Slf4j
public class ProductController {
    public final ProductService productService;
    
    /**
     * 根据id获取商品数据
     */
    @GetMapping("/{id}")
    public Result<ProductVO> getById(@PathVariable Long id){
        ProductVO product = productService.getById(id);
        if (product == null) {
            throw new BaseException("商品不存在或已被下架");
        }
        return Result.success(product);
    }
    /**
     * 分页查询商品
     */
    @GetMapping("/page")
    public Result<IPage<UserProductPageVO>> pageQuery(ProductQueryDTO requestParam){
        return Result.success(productService.pageQuery(requestParam,UserProductPageVO.class));

    }

}